<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta name="description" content="This is a coupon website">
    <link rel="shortcut icon" href="./imgs/favicon.png" type="image/x-icon">
    <link rel="stylesheet" href="./css/normal.css">
    <link rel="stylesheet" href="./css/index.css">
    <script src="./js/vue@2.js"></script>
    <title>Search</title>
    <script async="async" src="https://www.google.com/adsense/search/ads.js"></script>
    <script type="text/javascript" charset="utf-8">
        (function (g, o) {
            g[o] = g[o] || function () {
                (g[o]['q'] = g[o]['q'] || []).push(arguments)
            }
                ,
                g[o]['t'] = 1 * new Date
        }
        )(window, '_googCsa');
    </script>
</head>
<style>
    [v-cloak] {
        display: none;
    }
</style>

<body>
    <div id="app" v-cloak>
        <div class="header">
            <div class="header-content">
                <div class="header-content-normal">
                    <div class="header-content-search" @click="handleToSearch"></div>
                    <input class="header-content-search_input" type="text" v-model="pageOptions.query" placeholder="Search ...">
                    <div class="header-content-menu" @click="showMenu = true"></div>
                </div>
                <div class="nav" :class="{open: showMenu}">
                    <a class="nav-item" href="index.html"><span>Home</span></a>
                    <a class="nav-item" :href="`type.html?type=${encodeURIComponent(item)}`"
                        v-for="item in categories"><span>{{item}}</span></a>
                </div>
                <div class="nav-mask" v-show="showMenu" @click="showMenu = false"></div>
            </div>

            <div class="search">
                <input class="search_input" type="text" v-model="pageOptions.query" placeholder="Search ...">
                <div class="search-btn" @click="handleToSearch">
                    <img class="search-btn_img" src="./imgs/icon-search.png" alt="search_icon" width="20">
                </div>
            </div>
            
        </div>
        <div class="main">
            <ul class="cards" v-cloak>
                <li v-for="item in resultCoupons">
                    <div class="card">
                        <div class="card-top">
                            <div class="card-img">
                                <img :src="item.icon" :alt="item.title" width="140">
                            </div>
                            <div class="card-info">
                                <span class="card-info-title">{{item.title}}</span>
                                <span class="card-info-desc">{{item.descInfo}}</span>
                                <span class="card-info-tips">{{item.created}}</span>
                            </div>
                        </div>
                        <div class="card-bottom">
                            <span class="card-name">{{item.couponTypeName}}</span>
                            <a class="card-btn" :href="handleToDetail(item)">Get Deal</a>
                        </div>
                    </div>
                </li>
            </ul>
            <div id="afscontainer1" style="max-width: 1130px;width: 100%;margin: 0 auto;"></div>
            <!-- <div id="afscontainer2" style="max-width: 1130px;width: 100%;margin: 0 auto;"></div>
            <div id="afscontainer3" style="max-width: 1130px;width: 100%;margin: 0 auto;"></div> -->
        </div>
        <div class="footer">
            <div class="footer-nav">
                <a href="./cookies-policy.html">Cookies Policy</a>
                <a href="./privacy-policy.html">Privacy Policy</a>
                <a href="javascript:;">We updated our Terms of Service on Aug 4, 2023.</a>
                <a href="javascript:;">©2023 Clutch.co. All rights reserved.</a>
            </div>
        </div>
    </div>
</body>

<script src="./mock/data.js"></script>
<script>
    var vm = new Vue({
        el: '#app',
        data() {
            return {
                showMenu: false,
                categories: {
                    1: 'Electronics',
                    2: 'Auto Insurance',
                    3: 'Apparel&Shoes',
                    4: 'Beauty Makeup',
                    5: 'Toys Games'
                },
                pageOptions: {
                    pubId: "pub-9616389000213823",
                    styleId: "5290249905",
                    // styleId: "7475049824",
                    query: 'coupon'
                },
                adblock1: {
                    container: "afscontainer1"
                },
                pageOptions2: {
                    pubId: "pub-9616389000213823",
                    // "styleId": "5290249905",
                    styleId: "7475049824",
                    query: 'coupon type'
                },
                adblock2: {
                    container: "afscontainer2"
                },
                pageOptions3: {
                    pubId: "pub-9616389000213823",
                    // "styleId": "5290249905",
                    styleId: "7475049824",
                    query: 'coupon website rank'
                },
                adblock3: {
                    container: "afscontainer3"
                },
                keywords: '',
                resultCoupons: [],
                couponList: []
            }
        },
        created() {
            this.getCouponList()
            this.keywords = this.getQueryVariable('keywords') && decodeURIComponent(this.getQueryVariable('keywords'))
            this.pageOptions.query = this.keywords ? this.keywords : 'coupon'
        },
        mounted() {
            this.$nextTick(() => {
                this.handleToSearch()
                // _googCsa('ads', this.pageOptions2, this.adblock2);
                // _googCsa('ads', this.pageOptions3, this.adblock3);
            })
        },
        methods: {
            handleToDetail(coupon) {
                return `detail.html?data=${encodeURIComponent(JSON.stringify(coupon))}`
            },
            getCouponList() {
                this.loading = true
                fetch('https://api.92onegame.com/bibleApi/coupon/findCouponList', {
                    headers: {
                        'Content-Type': 'application/json; charset=utf-8'
                    },
                    method: 'post',
                    body: JSON.stringify({
                        limit:30,
                        page: 1
                    })
                })
                    .then(res => res.json())
                    .then(res => {
                        res.data.push(...BeautyMakeup, ...ApparelShoes, ...AutoInsurance, ...ToysGames)
                        this.couponList = res.data
                        this.handleToSearch()
                    })
                    .catch(err => {
                        throw new Error(err)
                    })
            },
            handleToSearch() {
                this.resultCoupons = this.keywords.length > 0 ? this.couponList.filter(item => {
                    return item.title.toLowerCase().indexOf(this.keywords.toLowerCase()) > -1
                }) : []

                _googCsa('ads', this.pageOptions, this.adblock1);
            },
            back() {
                window.history.back()
            },
            getQueryVariable(variable) {
                var query = window.location.search.substring(1);
                var vars = query.split("&");
                for (var i = 0; i < vars.length; i++) {
                    var pair = vars[i].split("=");
                    if (pair[0] == variable) { return pair[1]; }
                }
                return (false);
            }
        }
    })
</script>

</html>